<?php

namespace html\core;

Abstract class document
{

	protected $_html;
	protected $_tags;

	protected function setHtml($html)
	{
		$this->_html = $html;
	}

	function getHtmlSource()
	{
		return $this->getHtml();
	}

	function getHtml()
	{
		return $this->_html;
	}

	protected function clearTags()
	{
		$this->_tags = array();
	}

	protected function mergeTags($array)
	{
		if( $array ) foreach( $array as $tagId => $tag )
			{
				$this->_tags[$tagId] = $tag;
			}
	}

	protected function setTag($object)
	{
		$this->_tags[] = $object;
	}

	public function setTags($array)
	{
		$this->_tags = $array;
	}

	public function getTags()
	{
		return $this->_tags;
	}

	public function getNextSibling()
	{
		if( $this->_tags ) foreach( $this->_tags as $id => $tag )
			{
				if( $tag->hasSiblings() ) foreach( $tag->getSiblings() as $siblings )
					{
						$return[$siblings->getId()] = $siblings;
					}
			}
		return $return;
	}

	public function getAllChildren()
	{
		if( $this->_tags ) foreach( $this->_tags as $id => $tag )
			{
				if( $tag->hasChildren() ) foreach( $tag->getChildren() as $children )
					{
						$return[$children->getId()] = $children;
					}
			}
		return $return;
	}

	public function getEveryChildren()
	{
		if( $this->_tags ) foreach( $this->_tags as $id => $tag )
			{
				$children = $this->getChildrenFromTag($tag);
				if( $children )
				{
					foreach( $children as $childTag )
					{
						$return[$childTag->getId()] = $childTag;
					}
				}
//				echo $id." has: ".count($children)." children\n";
			}
		return $return;
	}

	public function getChildrenFromTag($tag, &$children=array())
	{
		if( $tag->hasChildren() )
		{
			foreach( $tag->getChildren() as $childrenTag )
			{
				$children[$childrenTag->getId()] = $childrenTag;
				$this->getChildrenFromTag($childrenTag, $children);
			}
		}
		return $children;
	}

	public function getTagCount()
	{
		return count($this->_tags);
	}

	public function getTagById($id)
	{
		if( $this->_tags ) foreach( $this->_tags as $arrayId => $tag )
			{
				if( $tag->getId() == $id )
				{
					return $tag;
				}
			}
	}

	public function removeTagById($id)
	{
		if( $this->_tags ) foreach( $this->_tags as $arrayId => $tag )
			{
				if( $tag->getId() == $id )
				{
					unset($this->_tags[$arrayId]);
				}
			}
	}

}

?>
